package com.king.cms.template;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException; 

import com.king.cms.action.BaseDispatchAction;
import com.king.cms.common.*;
import com.king.cms.db.QueryPage;
import com.king.cms.log.LogService;
 

public class TemplateAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(TemplateAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws ServletException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException {
		 
		String action = request.getParameter("action");
		 
		if("list".equals(action)){
			QueryPage query = new QueryPage(request,"template.do?action=list",InitSystem.getBpmParam("template.manage.sql"));
			request.setAttribute("query", query);
			return mapping.findForward("template.list");
		 } 
		 if("add".equals(action)){ 
			 return mapping.findForward("template.add");
		 } 
		 if("delete".equals(action)){ 
			 String[] templateIds = request.getParameterValues("templateId");
			 if(templateIds != null){
				 List useTemplate = new ArrayList();
				 HashMap map = new HashMap();
				 String sql = "select * from cms_delphi_template where id in(" + 
				 			   " select DISPLAY_TEMPLATE AS TEMPLATE from cms_client client " +
				 			   " union " +
				 			   " select DEFAULT_TEMPLATE AS TEMPLATE from cms_client client "+
				 			   " union " +
				 			   " select TEMPLATE_ID AS TEMPLATE FROM cms_meeting_client )";
				 List useList = this.getDBOperator(request).select(sql, null);
				 for(int j = 0;j<useList.size();j++){
					 Map m = (Map)useList.get(j);
					 map.put(m.get("ID")+"", m.get("DELPHI_TEMPLATE_NAME") + "");
				 }
				 for(int i=0;i<templateIds.length;i++){
					 String templateId = templateIds[i]; 
					 if( null != map.get(templateId) ){
						 useTemplate.add(map.get(templateId));
					 }else{
						 this.getDBOperator(request).update(InitSystem.getBpmParam("template.manage.delete"), new Object[]{new Integer(templateId)});
					 }
				 }
				 if(useTemplate.size() != 0){
					 request.setAttribute("useTemplate",useTemplate);
					 return mapping.findForward("template.del");
				 }
			 }
			 return new ActionForward("/template.do?action=list");
		 }
		 if("modify".equals(action)){ 
			 String templateId = request.getParameter("templateId"); 
			 List list = this.getDBOperator(request).select(InitSystem.getBpmParam("template.manage.sql_one"), new Object[]{new Integer(templateId)});
			 request.setAttribute("template", (Map)list.get(0));
			  
			 return mapping.findForward("template.modify");
		 }
		 if("save".equals(action)){  
			 try{
			 
			 String templateId = request.getParameter("templateId");
			 String fileType = request.getParameter("fileType");
			 String sql = InitSystem.getBpmParam("template.manage.update"); 
			 if(templateId == null)
				 sql = InitSystem.getBpmParam("template.manage.insert"); 
			 String templateName = request.getParameter("templateName");

			 String templateFile = request.getParameter("templateFile");
			 String fileSaveName = request.getParameter("fileSaveName");
			 
			 byte[] content = null;
			 FileRw f = new FileRw();
			 String fileFullPath = "http://"+InitSystem.getBpmParam("ip")+":"+InitSystem.getBpmParam("port")+request.getContextPath()+f.getFileSavePath("")+fileSaveName;
			 
			// String contentStr = ff.ReadFile(request.getRealPath("/")+InitSystem.getBpmParam("fileSavePath")+templateFile);
			  if("0".equals(fileType)){
				//  System.out.println(new File(request.getRealPath("/")+InitSystem.getBpmParam("fileSavePath")+fileSaveName));
			    FileInputStream in = new FileInputStream(new File(request.getRealPath("/")+InitSystem.getBpmParam("fileSavePath")+fileSaveName));
			   
				int length = in.available();
				byte[] byteContent = new byte[length];
				in.read(byteContent);
				in.close(); 
				content = byteContent;
				fileFullPath = fileSaveName;
			  }  
			 String templateDesc = request.getParameter("templateDesc");
			 //System.out.println("dddd:" + templateDesc);
			 //System.out.println("sql:" + sql);
			 if(templateId == null)
			   this.getDBOperator(request).update(sql, new Object[]{templateName,content,templateFile,templateDesc,fileType,fileFullPath});
			 else
				 this.getDBOperator(request).update(sql, new Object[]{templateName,content,templateFile,templateDesc,fileType,fileFullPath,new Integer(templateId)});
			 }catch(Exception e){
				 e.printStackTrace();
			 }
			 
			 return new ActionForward("/template.do?action=list");
		 }
		 if("download".equals(action)){ 
			 return new ActionForward("/template.do?action=list");
		 }
		 
		return mapping.findForward("listinfo");
	 
	}
}
